package com.shenzhen.horselive;


import android.text.TextUtils;

import com.flqy.baselibrary.IUser;
import com.google.gson.Gson;
import com.shenzhen.horselive.api.response.LoginResponse;
import com.shenzhen.horselive.api.response.MyInfoResponse;
import com.shenzhen.horselive.common.Events;
import com.shenzhen.horselive.common.SP;

import org.greenrobot.eventbus.EventBus;


/**
 * 当前登录角色信息
 */
public class User implements IUser {


    protected static final String QQ_AUTH_ID = "qq_auth_id";
    protected static final String Login_INFO = "login_info";
    protected static final String MY_INFO = "my_info";
    protected static final String IS_FIRST_OEPN = "is_first_oepn";
    protected static  final  String IS_LOGIN = "islogin";

    private SP sp;
    //第三方认证信息
    protected String qqAuthId;    //第三方认证信息
    protected boolean IsFirstOepn;    //第一次打开APP
    protected LoginResponse loginResponse; //登录信息
    protected MyInfoResponse myInfo; //我的信息

    //是否
    private  boolean isLogin;


    public static User get() {
        return App.getInstance().getUser();
    }

    private Gson gson;

    public User() {
        super();
        gson = new Gson();
        sp = new SP("USER_DATA");
        qqAuthId = sp.getString(QQ_AUTH_ID, "");
        IsFirstOepn = sp.getBoolean(IS_FIRST_OEPN, false);

    }


    public boolean isFirstOepn() {
        return IsFirstOepn;
    }

    public void setFirstOepn(boolean firstOepn) {
        IsFirstOepn = firstOepn;
        sp.putBoolean(IS_FIRST_OEPN, firstOepn);
    }

    public boolean isLogin() {
       return sp.getBoolean(IS_LOGIN,false);

    }

    public void setLogin(boolean login) {
       this.isLogin = login;
       sp.putBoolean(IS_LOGIN,login);
    }

    public void storeLoginInfo(LoginResponse data) {
        this.loginResponse = data;
        sp.putString(Login_INFO, gson.toJson(data));
    }

    public void storeMyInfo(MyInfoResponse data) {
        this.myInfo = data;
        sp.putString(MY_INFO, gson.toJson(data));
    }


    public MyInfoResponse getMyInfo() {
        if (myInfo == null) {
            String json = sp.getString(MY_INFO, "");
            if (!TextUtils.isEmpty(json)) {
                myInfo = gson.fromJson(json, MyInfoResponse.class);
            } else {
                myInfo = new MyInfoResponse();
            }
        }
        return this.myInfo;
    }


    public LoginResponse getStoreLoginInfo() {
        if (loginResponse == null) {
            String json = sp.getString(Login_INFO, "");
            if (!TextUtils.isEmpty(json)) {
                loginResponse = gson.fromJson(json, LoginResponse.class);
            } else {
                loginResponse = new LoginResponse();
            }
        }
        return this.loginResponse;
    }


    public void notifyChange(String... keys) {
        EventBus.getDefault().post(new Events.UserDataEvent(keys));
    }

    public void loginOut() {
        clear();
        notifyChange();
    }

    private void clear() {


        sp.remove(IS_FIRST_OEPN);
        loginResponse = null;

        sp.remove(IS_LOGIN);
        isLogin = false;

        sp.remove(MY_INFO);
        myInfo = null;



    }


    @Override
    public String getId() {
        return null;
    }
}


